package GUI.Paleta;

import BE.BEPaleta;
import BL.BLCombo;
import BL.BLPaleta;
import BL.BLValidaciones;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

public class GUIModificarPaleta extends javax.swing.JDialog {

    BEPaleta bePallet = null;
    /** Creates new form GUIModificarPallet */
    public GUIModificarPaleta(java.awt.Frame parent, boolean modal,int idPallet) {
        super(parent, modal);
        initComponents();
        this.inicializa();
        this.llenaVentana(idPallet);
    }

    public void inicializa()
    {
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
        BLCombo blCombo = new BLCombo();
        blCombo.setCombo("TIPOPALETA",1,cmbTipoPallet);
    }

    public void llenaVentana (int idPallet)
    {
        BLPaleta bLPallet = new BLPaleta();
        bePallet = bLPallet.getPaletabyID(idPallet);

        if (bePallet != null)
        {
            int i = 0;
            txtPallet.setText(bePallet.getNombre());
            txtarDescripcion.setText(bePallet.getDescripcion());

            for (i=0;i<cmbTipoPallet.getModel().getSize();i++)
            {
                BLCombo s = (BLCombo)cmbTipoPallet.getModel().getElementAt(i);
                if (s.id == bePallet.getIdTipoPallet())
                    break;
            }
            cmbTipoPallet.setSelectedIndex(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDatosPallet = new javax.swing.JPanel();
        lblPallet = new javax.swing.JLabel();
        txtPallet = new javax.swing.JTextField();
        lblTipoPallet = new javax.swing.JLabel();
        cmbTipoPallet = new javax.swing.JComboBox();
        lblDescripcion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtarDescripcion = new javax.swing.JTextArea();
        btnModificar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editar Paleta");
        setName("GUIModificarPallet"); // NOI18N

        pnlDatosPallet.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Pallet"));

        lblPallet.setText("Paleta:");

        lblTipoPallet.setText("Tipo de Paleta:");

        cmbTipoPallet.setEnabled(false);

        lblDescripcion.setText("Descripción");

        txtarDescripcion.setColumns(20);
        txtarDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtarDescripcion);

        javax.swing.GroupLayout pnlDatosPalletLayout = new javax.swing.GroupLayout(pnlDatosPallet);
        pnlDatosPallet.setLayout(pnlDatosPalletLayout);
        pnlDatosPalletLayout.setHorizontalGroup(
            pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPalletLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlDatosPalletLayout.createSequentialGroup()
                        .addComponent(lblPallet)
                        .addGap(68, 68, 68)
                        .addComponent(txtPallet))
                    .addGroup(pnlDatosPalletLayout.createSequentialGroup()
                        .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTipoPallet)
                            .addComponent(lblDescripcion))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmbTipoPallet, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlDatosPalletLayout.setVerticalGroup(
            pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPalletLayout.createSequentialGroup()
                .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPallet)
                    .addComponent(txtPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTipoPallet)
                    .addComponent(cmbTipoPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnModificar.setText("Aceptar");
        btnModificar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnModificar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlDatosPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatosPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        BLPaleta blPallet = new BLPaleta();
        BLValidaciones validar = new BLValidaciones();
        int retorno;
        boolean cambio = false;

        if (!bePallet.getNombre().equals(txtPallet.getText())) cambio = true;
        bePallet.setNombre(validar.validarSqlInjection(txtPallet.getText()));
        bePallet.setDescripcion(validar.validarSqlInjection(txtarDescripcion.getText()));

        retorno = blPallet.modificaPallet(bePallet,cambio);
        if (retorno ==0)
        {
            
            BL.BLLogAudit objGestorLog= new BL.BLLogAudit();
            BE.BELogAudit auxLogAudit= new BE.BELogAudit();
            auxLogAudit.setIdInterface(0);
            auxLogAudit.setIdUsuario(BL.BLCardex.getObjUsuario().getIdUsuario());
            auxLogAudit.setAccion("Edicion de la Paleta "+bePallet.getNombre()+" por el usuario "+BL.BLCardex.getObjUsuario().getUsuario()+"." );
            java.util.Date aux= new java.util.Date();
            java.sql.Timestamp fecha= new java.sql.Timestamp(aux.getTime());
            
            auxLogAudit.setFecha(fecha);
            
            objGestorLog.registrarAccion(auxLogAudit);

            
            
            
            JOptionPane.showMessageDialog(null,"Se ha editado el producto",
                                               "Informacion",JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnModificarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIModificarPaleta dialog = new GUIModificarPaleta(new javax.swing.JFrame(), true,0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JComboBox cmbTipoPallet;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblPallet;
    private javax.swing.JLabel lblTipoPallet;
    private javax.swing.JPanel pnlDatosPallet;
    private javax.swing.JTextField txtPallet;
    private javax.swing.JTextArea txtarDescripcion;
    // End of variables declaration//GEN-END:variables

}
